//
// Created by whg on 2025-06-01.
//

#ifndef PYVM_2025_BINARY_FILE_PARSER_HPP
#define PYVM_2025_BINARY_FILE_PARSER_HPP

#include "util/bufferedInputStream.hpp"
#include "codeObject.hpp"

class BinaryFileParser {
private:
    BufferedInputStream* _input;

    ArrayList<PyObject*> _cache; // 对象缓存
    ArrayList<PyString*> _stringTable; // 字符串常量缓存

    bool checkRefFlag(char objectType){
        return (objectType & 0x80) != 0;
    }

    // 将1字节的高位清零
    char clearHighBit(char objectType){
        return objectType & 0x7f;
    }

    CodeObject* getCodeObject();
    PyString* getByteCodes();
    PyString* getStrint(bool isLongString);

    PyList* getConsts();
    PyList* getNames();
    PyList* getVarNames();
    PyList* getFreeVars();
    PyList* getCellVars();

    PyList* tryToGetTuple();
    PyList* getTuple();

    PyString* getFileName();
    PyString* getName();

    PyString* getNoTable();

public:
    BinaryFileParser(BufferedInputStream *input);
    CodeObject* parse();

};


#endif //PYVM_2025_BINARY_FILE_PARSER_HPP
